/**
 * HttpUtil.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HttpUtil.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with google-api-translate-java.  If not, see <http://www.gnu.org/licenses/>.
 */
package me.http.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONObject;

/**
 *
 * @author Propietario
 */
public class HttpUtil {

    private static final String ENCODING = "UTF-8";

    public static byte[] loadHttpFile(HttpConnection hc) throws IOException {
        byte[] byteBuffer;

        try {
            hc.setRequestMethod(HttpConnection.GET);
            InputStream is = hc.openInputStream();
            try {
                int len = (int) hc.getLength();
                if (len > 0) {
                    byteBuffer = new byte[len];
                    int done = 0;
                    while (done < len) {
                        done += is.read(byteBuffer, done, len - done);
                    }
                } else {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[512];
                    int count;
                    while ((count = is.read(buffer)) >= 0) {
                        bos.write(buffer, 0, count);
                    }
                    byteBuffer = bos.toByteArray();
                }
            } finally {
                is.close();
            }
        } finally {
            hc.close();
        }

        return byteBuffer;
    }

    /**
     * Forms an HTTP request and returns the result of the request as a JSONObject.
     *
     * @param url The URL to query for a JSONObject.
     * @return The translated String.
     * @throws Exception on error.
     */
    public static JSONObject retrieveJSON(final String url, final String referrer) throws Exception {
        try {
            final HttpConnection hc = (HttpConnection) Connector.open(url);
            hc.setRequestProperty("referer", referrer);


            try {
                final String result = new String(loadHttpFile(hc), ENCODING);

                return new JSONObject(result);
            } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
            }
        } catch (Exception ex) {
            throw new Exception("[google-api-translate-java] Error retrieving translation.");
        }
    }
}
